﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace ARS.Common.Helper
{

    public class MailSenderHelper
    {
        private static SmtpSetting _smtp=null;
        public static void Init(SmtpSetting smtpconfig)
        {
            _smtp = smtpconfig;
        }


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="setting"></param>
        /// <param name="recipient">接收者</param>
        /// <param name="cc">抄送</param>
        /// <param name="bcc">密送</param>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="isBodyHtml">是否HTML</param>
        /// <param name="files">附件</param>
        public static void Send(SmtpSetting setting, string[] recipient,string[] cc,string[] bcc, string subject,string body, string[] files = null, bool isBodyHtml=true)
        {
            if(setting==null)
            {
                setting = _smtp;
            }
            SmtpClient smtpClient = new SmtpClient(setting.Server)
            {
                Port = setting.Port
            };
            if (setting.Port != 25)
            {
                smtpClient.EnableSsl = true;
            }
            //sender, recipient,subject,body
            MailMessage message = new MailMessage()
            {
                IsBodyHtml = isBodyHtml,
                SubjectEncoding = Encoding.UTF8,
                BodyEncoding = Encoding.UTF8, 
            };
            message.Subject = subject;
            message.Body = body;
            message.Sender= new MailAddress(setting.User, setting.Name, Encoding.UTF8);
            message.From = new MailAddress(setting.User, setting.Name, Encoding.UTF8);//发件人邮箱地址，名称，编码UTF8   ;
           
            if (recipient != null && recipient.Length > 0)
            {
                foreach (var info in recipient)
                {
                    message.To.Add(info);
                }
            }
            if (cc != null && cc.Length > 0)
            {
                foreach (var info in cc)
                {
                    message.CC.Add(info);
                }
            }
            if (bcc != null && bcc.Length > 0)
            {
                foreach (var info in bcc)
                {
                    message.Bcc.Add(info);
                }
            }
            message.Attachments.Clear();
            if (files != null && files.Length>0)
            {
                for (int i = 0; i < files.Length; ++i)
                {
                    Attachment attach = new Attachment(files[i]);
                    message.Attachments.Add(attach);
                }
            }
            if (setting.Authentication)
            {
                smtpClient.Credentials = new NetworkCredential(setting.User, setting.Password);
            }
            smtpClient.Send(message);
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="recipient"></param>
        /// <param name="cc"></param>
        /// <param name="bcc"></param>
        /// <param name="titile"></param>
        /// <param name="body"></param>
        public static void Send(string[] recipient,string[] cc,string[] bcc , string titile, string body)
        {
            Send(null, recipient, cc, bcc, titile, body);
        }
    }

    /// <summary>
    /// SMTP 设置
    /// </summary>
    public class SmtpSetting
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string Server{get;set;}
        /// <summary>
        /// 是否验证
        /// </summary>
        public bool Authentication { get; set; } = true;
        /// <summary>
        /// 用户邮箱 发送者
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// 发送者昵称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 端口
        /// </summary>
        public int Port { get; set; } = 25;
    }
}
